<template>
	<view class="container">
		<view style="padding-left:15px; padding-right: 15px; height: 32px; line-height: 32px; font-size:14px;">请选择支付方式</view>
		<view class="vc-item" @click="chooseWechat(1)" >
			<image class="vc-item-icon" src="../../static/images/chat@3x.png"></image>
			<text class="vc-item-title">微信支付</text>
			<image v-if="paymentType == 1" class="vc-item-arrow" src="../../static/images/dui_4@3x.png"></image>
		</view>
	<!-- 	<view class="vc-item" @click="chooseHeMaPay(heMaPaymentType)">
			<image class="vc-item-icon" src="../../static/images/icon_qrcode.png"></image>
			<text class="vc-item-title">{{ heMaPaymentTxt }}</text>
			<image v-if="paymentType == heMaPaymentType" class="vc-item-arrow" src="../../static/images/dui_4@3x.png"></image>
		</view> -->
		<!-- <view class="vc-item" v-if="orderType != 60 && orderType != 80" @click="chooseBalance(4)">
			<image class="vc-item-icon" src="../../static/images/menoy@3x.png"></image>
			<text class="vc-item-title">账户余额支付</text>
			<image v-if="paymentType == 4" class="vc-item-arrow" src="../../static/images/dui_4@3x.png"></image>
		</view> -->
		<payment :payShow="payShow" :payStatus="payStatus" :payTitle="payTitle" @paySubmit="balancePay()" @payClose="payShow = false"></payment>
		<button class="confirm-btn-submit bottom-change-view" :loading="submitUploadStatus" :disabled="submitUploadStatus" @click="$noMultipleClicks(paymentOrder)">
			{{ paymentTxt }} {{ paymentInfo.totalAmount | moneyFormat(2) }}元
		</button>
	</view>
</template>

<script>
import { successToShow, errorToShow } from '@/config/common.js';
import { createPayment } from '@/constant/wechatpay.js';
import payment from '@/components/customer-payment/payment.vue';
import { checkAccountPayOrder } from '@/constant/home.js';
import { updatePlayerOrderStatusTemp } from '@/constant/customer.js';
export default {
	components: {
		payment
	},
	data() {
		return {
			payShow: false,
			payStatus: false,
			submitUploadStatus: false,
			payTitle: '请输入支付密码',
			orderType: '',
			paymentType: 1,
			paymentTxt: '微信支付',
			heMaPaymentType: 5,
			heMaPaymentTxt: '在线支付',
			paymentInfo: {
				CustomerId: '',
				OrderNo: '',
				Channel: 4,
				totalAmount: 0,
				PaymentType: 1
			},
			Prepay_Id: '',
			wechatRequest: {
				AppId: '',
				TimeStamp: '',
				NonceStr: '',
				Package: '',
				SignType: '',
				PaySign: ''
			},
			_orderNo: '',
			noClick: true
		};
	},
	onLoad(option) {
		if (option.orderNo ) {
			this.paymentInfo.customerId = uni.getStorageSync('customerId');
			this.paymentInfo.orderNo = option.orderNo;
			this.paymentInfo.totalAmount = option.orderAmount;
			// this.orderType = option.orderType;
			this._orderNo = option.orderNo;
			this.initPagePaymentType();
		}
	},
	methods: {
		initPagePaymentType() {
			this.paymentType = 1;
			this.paymentInfo.paymentType = 1;
			this.paymentTxt = this.paymentTxt;
			// if (!['10', '90'].includes(this.orderType)) {
			// 	this.paymentType = this.heMaPaymentType;
			// 	this.paymentInfo.PaymentType = this.heMaPaymentType;
			// 	this.paymentTxt = this.heMaPaymentTxt;
			// }
		},
		chooseWechat(_type) {
			this.paymentType = _type;
			this.paymentInfo.PaymentType = _type;
			this.paymentTxt = '微信支付';
		},
		chooseBalance(_type) {
			this.paymentType = _type;
			this.paymentInfo.PaymentType = _type;
			this.paymentTxt = '余额支付';
		},
		chooseHeMaPay(_type) {
			this.paymentType = _type;
			this.paymentInfo.PaymentType = _type;
			this.paymentTxt = this.heMaPaymentTxt;
		},

		async paymentOrder() {
			// if (this.paymentInfo.TotalAmount <= 0.0) {
			// 	errorToShow('支付金额不能小于等于0元');
			// 	return;
			// }

			if (this.paymentType == 1) {
				this.wechatPay();
			} else if (this.paymentType == this.heMaPaymentType) {
				this.hemaPay();
			} else {
				let hasPwd = uni.getStorageSync('customer_password');
				if (!hasPwd) {
					errorToShow('请设置交易密码', res => {
						setTimeout(() => {
							uni.navigateTo({
								url: '../mine/setting/index'
							});
						}, 1000);
					});
					return;
				}
				if (this.orderType == 60) {
					errorToShow('vip订单不能使用余额支付');
					return;
				} else {
					this.showBalancePay();
				}
			}
		},
		async hemaPay() {
			try {
				this.submitUploadStatus = true;
				
				let result = await createPayment(params);
				let hemaPayRequest = JSON.parse(result.HeMaPayData);
				wx.requestPayment({
					appId: hemaPayRequest.appId,
					timeStamp: hemaPayRequest.timeStamp,
					nonceStr: hemaPayRequest.nonceStr,
					package: hemaPayRequest.package,
					signType: hemaPayRequest.signType,
					paySign: hemaPayRequest.paySign,
					success(res) {
						successToShow('支付成功', res => {
							uni.switchTab({
								url: '../order/index'
							});
						});
					},
					fail(err) {
						console.log(err);
						errorToShow('支付失败，请重试');
					}
				});
			} catch (e) {}
			this.submitUploadStatus = false;
		},

		async wechatPay() {
			try {
				this.submitUploadStatus = true;
				let params={
					"orderNo":this._orderNo,
					"type":4,
					payType:10,
					customerId:uni.getStorageSync('customerId'),
					amount:this.paymentInfo.totalAmount
				}
				let result = await createPayment(params);
				this.Prepay_Id = result.Prepay_Id;
				this.wechatRequest =JSON.parse(result.data.pay_info) ;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: this.wechatRequest.timeStamp,
					nonceStr: this.wechatRequest.nonceStr,
					package:  this.wechatRequest.package,
					signType: this.wechatRequest.signType,
					paySign: this.wechatRequest.paySign,
					success(res) {
						successToShow('支付成功', res => {
							uni.switchTab({
								url: '../order/index'
							});
						});
					},
					fail(err) {
						console.log(err);
						errorToShow('支付失败，请重试');
					}
				});
			} catch (e) {}
			this.submitUploadStatus = false;
		},
		showBalancePay() {
			this.payShow = true;
			this.payEntry = true;
		},
		async balancePay(input_pwd) {
			try {
				if (input_pwd.length != 6) {
					errorToShow('请输入6位密码');
					return;
				}
				this.payShow = false;
				this.submitUploadStatus = true;
				let result = await createPayment(this.paymentInfo);
				let accountPayInfo = {
					orderNo: this.paymentInfo.orderNo,
					customerId: this.paymentInfo.customerId,
					CustomerPassword: input_pwd
				};
				let result2 = await checkAccountPayOrder(accountPayInfo);
				successToShow('支付成功', res => {
					uni.switchTab({
						url: '../order/index'
					});
				});
			} catch (e) {}
			this.submitUploadStatus = false;
		}
	}
};
</script>

<style scoped lang="scss">
.container {
	background-color: $main-page-color;
	height: 100vh;
}

.vc-item {
	display: flex;
	align-items: center;
	font-size: $main-text-size-32;
	color: $main-text-color-gray-33;
	height: 120rpx;
	background-color: #fff;
	margin-bottom: 20rpx;

	&-icon {
		margin-left: 24rpx;
		width: 64rpx;
		height: 64rpx;
		margin-right: 30rpx;
	}

	&-arrow {
		width: 30rpx;
		height: 30rpx;
		margin-right: 24rpx;
	}

	&-title {
		margin-right: auto;
	}
}

.confirm-btn-submit {
	position: fixed;
	bottom: 0;
	background-color: $main-theme-color-green;
	font-size: $main-text-size-32;
	color: #fff;
	width: 100vw;
	line-height: 120rpx;
	height: 120rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	border-radius: unset;
}
</style>
